package com.swjd.jingle.fragment;

import android.content.Intent;
import android.os.Process;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.swjd.jingle.R;
import com.swjd.jingle.activity.GuideActivity;
import com.swjd.jingle.activity.LoginActivity;
import com.swjd.jingle.adapter.SetAdapter;
import com.xuexiang.xui.widget.button.RippleView;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class SetFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.lv1)
    ListView lv1;
    @BindView(R.id.lv2)
    ListView lv2;
    @BindView(R.id.lv3)
    ListView lv3;
    @BindView(R.id.rv_quit)
    RippleView rvQuit;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private String[] names1, names2, names3;
    private SetAdapter setAdapter1, setAdapter2, setAdapter3;
    private BottomSheet.BottomListSheetBuilder bottomBuilder;

    @Override
    public void initialize() {
        names1 = new String[]{"账号绑定", "个人资料", "修改密码", "切换账号"};
        names2 = new String[]{"清除缓存", "意见反馈", "清空聊天记录"};
        names3 = new String[]{"欢迎页", "用户协议", "隐私政策", "检查更新"};
        setAdapter1 = new SetAdapter(activity, names1);
        setAdapter2 = new SetAdapter(activity, names2);
        setAdapter3 = new SetAdapter(activity, names3);
    }

    @Override
    public void initViews() {
        tvTitle.setText(getResources().getString(R.string.set));
        lv1.setAdapter(setAdapter1);
        lv2.setAdapter(setAdapter2);
        lv3.setAdapter(setAdapter3);
    }

    @Override
    public void initData() {
        lv1.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0:

                    break;
                case 1:
                    fragmentManager.beginTransaction().replace(R.id.layout_main, new UserInfoFragment())
                            .addToBackStack(null)
                            .commit();
                    break;
                case 2:

                    break;
                case 3:

                    break;
            }
        });
        lv2.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0:

                    break;
                case 1:

                    break;
                case 2:

                    break;
            }
        });
        lv3.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 0:
                    Intent intent = new Intent(activity, GuideActivity.class);
                    intent.putExtra("isOne", false);
                    startActivity(intent);
                    break;
                case 1:

                    break;
                case 2:

                    break;
                case 3:

                    break;
            }
        });
        bottomBuilder = new BottomSheet.BottomListSheetBuilder(getActivity());
        bottomBuilder
                .addItem("退出登录")
                .addItem("关闭叮当")
                .addItem("取消")
                .setIsCenter(true)
                .setOnSheetItemClickListener((dialog, itemView, position, tag) -> {
                    switch (position) {
                        case 0:
                            dialog.dismiss();
                            //确认对话框
                            DialogLoader.getInstance().showConfirmDialog(activity, "请问是否确认退出登录", "是", (dialog1, which) -> {
                                editor.putString("userInfo", "");
                                editor.commit();
                                app.setUserInfo(null);
                                startActivity(new Intent(activity, LoginActivity.class));
                                activity.finish();
                            }, "否");
                            break;
                        case 1:
                            dialog.dismiss();
                            //确认对话框
                            DialogLoader.getInstance().showConfirmDialog(activity, "请问是否确认退出应用", "是", (dialog1, which) -> {
                                Process.killProcess(Process.myPid());   //获取PID
                                System.exit(0);   //常规java、c#的标准退出法，返回值为0代表正常退出
                            }, "否");
                            break;
                        case 2:
                            dialog.dismiss();
                            break;
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_set;
    }

    @Override
    @OnClick({R.id.img_back, R.id.rv_quit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        fragmentManager.popBackStack();
                    }
                }, 500);
                break;
            case R.id.rv_quit:
                bottomBuilder.build().show();
                break;
        }
    }
}
